package com.jumi.microservice.material.service.impl;

import com.jumi.microservice.material.domain.JmWarehouse;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseRequest;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseResponse;
import com.jumi.microservice.material.mapper.JmWarehouseMapper;
import com.jumi.microservice.material.service.JmWarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ssk
 * @create 2020/7/21 8:45
 **/
@Service
public class JmWarehouseServiceImpl implements JmWarehouseService {

    @Resource
    private JmWarehouseMapper jmWarehouseMapper;

    @Override
    public int save(JmWarehouse jmWarehouse) {
        int count = jmWarehouseMapper.findCountByWarehouseName(jmWarehouse.getWarehouseName(),null);
        return count > 0 ? 0 : jmWarehouseMapper.save(jmWarehouse);
    }

    @Override
    public int update(JmWarehouse jmWarehouse) {
        int count = jmWarehouseMapper.findCountByWarehouseName(jmWarehouse.getWarehouseName(),jmWarehouse.getWarehouseId());
        return count > 0 ? 0 : jmWarehouseMapper.update(jmWarehouse);
    }

    @Override
    public JmWarehouseResponse findById(Long warehouseId) {
        JmWarehouseResponse rep = jmWarehouseMapper.findById(warehouseId);
        if (rep != null) {
            if (rep.getProvinceId() != null) {
                rep.setProvinceName(cbAreaName(rep.getProvinceId()));
            }
            if (rep.getCityId() != null) {
                rep.setCityName(cbAreaName(rep.getCityId()));
            }
            if (rep.getAreaId() != null) {
                rep.setAreaName(cbAreaName(rep.getAreaId()));
            }
        }
        return rep;
    }

    private String cbAreaName(Long areaId) {
        String areaName = jmWarehouseMapper.findAreaName(areaId);
        return areaName;
    }

    @Override
    public List<JmWarehouseResponse> findAll(JmWarehouseRequest jmWarehouse) {
        return jmWarehouseMapper.findAll(jmWarehouse);
    }

    @Override
    public List<JmWarehouseResponse> getWarehouseList() {
        return jmWarehouseMapper.getWarehouseList();
    }

    @Override
    public List<Map<String, Object>> findArea(Integer deep, Long parentId) {
        return jmWarehouseMapper.findArea(deep,parentId);
    }

    @Override
    public Integer getWarehouseIdByWarehouseName(String warehouseName)
    {
        return jmWarehouseMapper.getWarehouseIdByWarehouseName(warehouseName);
    }

	@Override
	public String getWarehouseNameByWarehouseId(Integer warehouseId)
	{

	    return jmWarehouseMapper.getWarehouseNameByWarehouseId(warehouseId);
	}
}
